package com.wealth.commodity.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Category;
import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.enums.GoodsStatusEnum;
import com.wealth.commodity.repository.CategoryDao;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.service.ICommentService;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.commodity.vo.CategoryVO;
import com.wealth.commodity.vo.GoodsVO;
import com.wealth.merchant.enums.MarketEnum;
import com.wealth.merchant.service.IBusinessDistrictService;
import com.wealth.merchant.vo.DistrictVO;
import com.wealth.merchant.vo.MerchantVO;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import iaas.utils.ResultObject;



/**
 * 本地购商品
 * @author Administrator
 */
@Controller
@RequestMapping(value="/localcommodity")
public class LocalGoodsController extends BaseController{
	
	Logger logger = Logger.getLogger(LocalGoodsController.class);
	@Autowired 
	private ICategoryService categoryService;
	@Autowired 
	private IGoodsService goodsService;
	@Autowired 
	private ICommentService commentService;
	@Autowired
	private IBusinessDistrictService districtService;
	@Autowired
	private CategoryDao categoryDao;
	
	/** 
	 * 商品列表页面
	 */
	@RequestMapping
	public String listForm(Model model,HttpServletRequest request,
			@RequestParam(value="categoryid",required=false) Long categoryid,
			@RequestParam(value="districtid",required=false) Long districtid) {
		try{
			if(categoryid==null){
				categoryid = -1L;
			}
			if(districtid==null){
				districtid = -1L;
			}
			//商圈
			List<DistrictVO> districtVOList = districtService.getBusinessDistrictForHuiLife(StatusEnum.NORMAL.getValue());
			model.addAttribute("districtVOList", districtVOList);
			
			//分类
			List<CategoryVO> categoryVOList = categoryService.getCategoryForHuiLife(1L, MarketEnum.LOCAL.getValue(),  StatusEnum.NORMAL.getValue());
			model.addAttribute("categoryVOList", categoryVOList);
			
			model.addAttribute("categoryid", categoryid);
			model.addAttribute("districtid", districtid);
			
			model.addAttribute("marketId",  MarketEnum.LOCAL.getValue());
			
			return "commodity/bdindex";
		}catch(Exception e){
			return "system/500";
		}
	}
	
	/** 
	 * 商户列表
	 */
	@RequestMapping(value="/more",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject list(
			@RequestParam(defaultValue="-1L") Long categoryid,
			@RequestParam(defaultValue="-1L") Long districtid,
			@RequestParam(defaultValue="0") Integer index,
			@RequestParam(defaultValue="10") Integer offset) {
		try{
			/** ---- 组装vo ---*/
			//判断分类是否父节点，影响Dao层查询
			Category category = categoryDao.findOne(categoryid);
			Integer isParentCategory = 0;
			if(categoryid!=-1 && category.getPcategory()==null){
				isParentCategory = 1;
			}
			//定义商品vo
			List<GoodsVO> goodsVOs=new ArrayList<GoodsVO>();
			//查询商品列表
			List<Goods> goodsList = goodsService.ajaxGoodsVO(MarketEnum.LOCAL.getValue(), categoryid, districtid, isParentCategory, GoodsStatusEnum.UP.getValue(), index, offset);
			//组合商品VO
			for (Goods goods : goodsList) {
				GoodsVO vo=new GoodsVO();
				vo.setGoods(goods);
				vo.setScore(commentService.getAvgScore(goods.getId()));
				goodsVOs.add(vo);
			}
			return new ResultObject("000", "商户列表加载成功", goodsVOs);
		}catch(Exception e){
			return new ResultObject("999", "商户列表加载错误", "");
		}
	}
	
	
	
}
